from sqlalchemy import Column, Integer, String, Sequence, DateTime, JSON, Enum
from sqlalchemy.orm import DeclarativeBase
from datetime import datetime

def one(object):
    tmp = {}
    for c in object.__table__.columns:
        col = getattr(object, c.name)
        if isinstance(col, datetime):
            tmp[c.name] = col.strftime("%Y-%m-%d %H:%M:%S")
        else:
            tmp[c.name] = col
    return tmp
def to_one(object):
    tmp = {}
    if isinstance(object, tuple):
        for tu in object:
            tmp = {**tmp, **one(tu)}
    else:
        tmp = one(object)
    return tmp

class Base(DeclarativeBase):
    def to_dict(self):
        return to_one(self)
    
    def mapping_col(self, data, *args):
        for a in args:
            if hasattr(self, a.lower()):
                setattr(self, a.lower(), data[a])
            elif hasattr(self, a):
                setattr(self, a, data[a])

class PetType(Base):
    __tablename__ = 'pet_type'
    
    pet_type_id = Column(Integer, primary_key=True, comment="精灵属系id")
    pet_type_name = Column(String(50), nullable=False, comment="精灵属系名称")
    
    sub_type1 = Column(Integer, index=True, comment="双属系相关属系1")
    sub_type2 = Column(Integer, index=True, comment="双属系相关属系2")

class Pet(Base):
    __tablename__ = 'pet'

    pet_id = Column(Integer, primary_key=True, comment="精灵id")
    pet_origin_id = Column(Integer, index=True, comment="实际精灵序号")
    pet_name = Column(String(50), nullable=False, comment="精灵名称")
    pet_type = Column(Integer, index=True, nullable=False, comment="精灵属系")
    
    ex_icon = Column(Integer, comment="额外魂印（觉醒，羁绊）")
    ex_icon_text = Column(String(50), comment="额外魂印文本")
    ex_skill = Column(Integer, comment="额外技能")
    ex_skill_text = Column(String(50), comment="额外技能名")
    
    head_hs = Column(String(100), comment="带有哈希值的真正头像编码")
    ex_mark = Column(Integer, comment="标记是否有进阶版本")
    
class Suit(Base):
    __tablename__ = 'suit'
    
    suit_id = Column(Integer, primary_key=True, comment="套装id")
    suit_name = Column(String(50), nullable=False, comment="套装名称")
    
    
    
class RaceGroup(Base):
    __tablename__ = 'race_group'

    group_id = Column(Integer, primary_key=True, comment="比赛组id")
    group_name = Column(String(50), nullable=False, comment="比赛组名称")
    
class RaceGroupPetBanHide(Base):
    __tablename__ = 'race_group_pet_ban_hide'
    
    group_id = Column(Integer, primary_key=True, comment="比赛组id")
    pet_id = Column(Integer, primary_key=True, comment="精灵id")
    
    is_banned = Column(Integer, default=0, comment="该精灵是否被公ban")
    is_hide_enable = Column(Integer, default=1, comment="该精灵是否可以暗手")
    
class RaceGroupPetTypeHide(Base):
    __tablename__ = 'race_group_pet_type_hide'
    
    group_id = Column(Integer, primary_key=True, comment="比赛组id")
    pet_type_id = Column(Integer, primary_key=True, comment="精灵属系id")
    
    is_hide_enable = Column(Integer, default=1, comment="该属系下是否可以暗手")
    
class UserInfo(Base):
    __tablename__ = 'user_info'

    user_id = Column(Integer, primary_key=True, comment="用户id")
    user_name = Column(String(50), nullable=False, comment="用户名称")
    pass_word = Column(String(256), nullable=False, comment="密码")

class UserPetFavorite(Base):
    __tablename__ = 'user_pet_favorite'

    user_id = Column(Integer, primary_key=True, comment="用户id")
    pet_id = Column(Integer, primary_key=True, comment="精灵id")

    
    